package cat.pipo.bicing.bean;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.zip.GZIPInputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpUtil {

	public ArrayList getAll() throws ClientProtocolException, IOException, HttpException {

		DefaultHttpClient httpclient = new DefaultHttpClient();

		// Prepare a request object
		HttpGet httpmethod = new HttpGet("http://rocboronat.net/barcelonabicing/bcnG?all=1");

		// Execute the request
		HttpResponse response;

		ArrayList stations = null;

		try {
			response = httpclient.execute(httpmethod);
			HttpEntity entity = response.getEntity();

			if (entity != null) {
				InputStream instream = entity.getContent();
				GZIPInputStream gzip = new GZIPInputStream(instream);
				ObjectInputStream ois = new ObjectInputStream(gzip);
				stations = (ArrayList) ois.readObject();
				instream.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return stations;
	}
	
	public HashMap getTiny() throws ClientProtocolException, IOException, HttpException {

		DefaultHttpClient httpclient = new DefaultHttpClient();

		// Prepare a request object
		HttpGet httpmethod = new HttpGet("http://rocboronat.net/barcelonabicing/bcnG");

		// Execute the request
		HttpResponse response;

		HashMap<Short,StationTiny> stationsTiny = null;

		try {
			response = httpclient.execute(httpmethod);
			HttpEntity entity = response.getEntity();

			if (entity != null) {
				InputStream instream = entity.getContent();
				GZIPInputStream gzip = new GZIPInputStream(instream);
				ObjectInputStream ois = new ObjectInputStream(gzip);
				stationsTiny = (HashMap<Short, StationTiny>) ois.readObject();
				instream.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return stationsTiny;
	}
	
	public void ping(String url) throws ClientProtocolException, IOException, HttpException {

		DefaultHttpClient httpclient = new DefaultHttpClient();

		// Prepare a request object
		HttpGet httpmethod = new HttpGet(url);

		// Execute the request
		HttpResponse response;

		try {
			response = httpclient.execute(httpmethod);
			HttpEntity entity = response.getEntity();

			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}